﻿Imports CrystalDecisions.CrystalReports.Engine

Public Class reports2




    Private Sub Ch_B_Name_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Ch_B_Name.CheckedChanged


        If Ch_B_Name.Checked = True Then
            C_B_G.Enabled = True
        Else
            C_B_G.Enabled = False
        End If

    End Sub

    Private Sub Ch_B_Date_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Ch_B_Date.CheckedChanged
        If Ch_B_Date.Checked = True Then
            Date_From.Enabled = True
            Date_To.Enabled = True
            cbTimeSearch.Enabled = True
        Else
            Date_From.Enabled = False
            Date_To.Enabled = False
            cbTimeSearch.Enabled = False
            cbTimeSearch.Checked = False
        End If

    End Sub

    Private Sub Btn_Search_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_Search.Click

        Dim TimeFrom, TimeTo As String
        If cbTimeSearch.Checked = True Then
            TimeFrom = " " & tpFrom.Value.Hour & ":" & tpFrom.Value.Minute
            TimeTo = " " & tpTo.Value.Hour & ":" & tpTo.Value.Minute
        Else
            TimeFrom = " 00:00"
            TimeTo = " 23:59"
        End If


        Dim dt As DataTable
        If Ch_B_Name.Checked = False And Ch_B_Date.Checked = False Then
            dt = GetTable("SELECT * FROM vwReport2")
            Dim rpt As New ReportDocument
            Try
                rpt.Load("CR2.rpt")
                rpt.SetDataSource(dt)
                CrystalReportViewer1.ReportSource = rpt
            Catch ex As Exception
                MessageBox.Show("Error: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try

        End If


        If Ch_B_Name.Checked = True And Ch_B_Date.Checked = False Then
            dt = GetTable("SELECT * FROM vwReport2 WHERE BatchID='" & GetTable("SELECT * FROM Batchs WHERE BatchName='" & C_B_G.Text & "'").Rows(0).Item("BatchID") & "'")
            Dim rpt As New ReportDocument
            Try
                rpt.Load("CR2.rpt")
                rpt.SetDataSource(dt)
                CrystalReportViewer1.ReportSource = rpt
            Catch ex As Exception
                MessageBox.Show("Error: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
        ElseIf Ch_B_Date.Checked = True And Ch_B_Name.Checked = False Then
            dt = GetTable("SELECT * FROM vwReport2 WHERE Date >= '" & Date_From.Value.Year & "-" & Date_From.Value.Month & "-" & Date_From.Value.Day & TimeFrom & "' and Date <= '" & Date_To.Value.Year & "-" & Date_To.Value.Month & "-" & Date_To.Value.Day & TimeTo & "'")
            Dim rpt As New ReportDocument
            Try
                rpt.Load("CR2.rpt")
                rpt.SetDataSource(dt)
                CrystalReportViewer1.ReportSource = rpt
            Catch ex As Exception
                MessageBox.Show("Error: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try

        ElseIf Ch_B_Date.Checked = True And Ch_B_Name.Checked = True Then
            dt = GetTable("SELECT * FROM vwReport2 WHERE BatchID='" & GetTable("SELECT * FROM Batchs WHERE BatchName='" & C_B_G.Text & "'").Rows(0).Item("BatchID") & "' and Date >= '" & Date_From.Value.Year & "-" & Date_From.Value.Month & "-" & Date_From.Value.Day & TimeFrom & "' and Date <= '" & Date_To.Value.Year & "-" & Date_To.Value.Month & "-" & Date_To.Value.Day & TimeTo & "'")
            Dim rpt As New ReportDocument
            Try
                rpt.Load("CR2.rpt")
                rpt.SetDataSource(dt)
                CrystalReportViewer1.ReportSource = rpt
            Catch ex As Exception
                MessageBox.Show("Error: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
        End If


    End Sub


    Private Sub reports_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim dt As DataTable
        dt = GetTable("SELECT * FROM vwBatchs")
        C_B_G.DataSource = dt
        C_B_G.DisplayMember = "BatchName"
        C_B_G.ValueMember = "BatchName"
    End Sub


    Private Sub btnGenerateReport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGenerateReport.Click
        Dim TimeFrom, TimeTo As String
        If cbTimeSearch.Checked = True Then
            TimeFrom = " " & tpFrom.Value.Hour & ":" & tpFrom.Value.Minute
            TimeTo = " " & tpTo.Value.Hour & ":" & tpTo.Value.Minute
        Else
            TimeFrom = " 00:00"
            TimeTo = " 23:59"
        End If


        Dim dt As DataTable
        If Ch_B_Name.Checked = False And Ch_B_Date.Checked = False Then
            dt = GetTable("SELECT * FROM vwReport2")
            Generate_report(dt, "CR2.rpt")

        End If


        If Ch_B_Name.Checked = True And Ch_B_Date.Checked = False Then
            dt = GetTable("SELECT * FROM vwReport2 WHERE BatchID='" & GetTable("SELECT * FROM Batchs WHERE BatchName='" & C_B_G.Text & "'").Rows(0).Item("BatchID") & "'")
            Generate_report(dt, "CR2.rpt")
        ElseIf Ch_B_Date.Checked = True And Ch_B_Name.Checked = False Then
            dt = GetTable("SELECT * FROM vwReport2 WHERE Date >= '" & Date_From.Value.Year & "-" & Date_From.Value.Month & "-" & Date_From.Value.Day & TimeFrom & "' and Date <= '" & Date_To.Value.Year & "-" & Date_To.Value.Month & "-" & Date_To.Value.Day & TimeTo & "'")
            Generate_report(dt, "CR2.rpt")

        ElseIf Ch_B_Date.Checked = True And Ch_B_Name.Checked = True Then
            dt = GetTable("SELECT * FROM vwReport2 WHERE BatchID='" & GetTable("SELECT * FROM Batchs WHERE BatchName='" & C_B_G.Text & "'").Rows(0).Item("BatchID") & "' and Date >= '" & Date_From.Value.Year & "-" & Date_From.Value.Month & "-" & Date_From.Value.Day & TimeFrom & "' and Date <= '" & Date_To.Value.Year & "-" & Date_To.Value.Month & "-" & Date_To.Value.Day & TimeTo & "'")
            Generate_report(dt, "CR2.rpt")
        End If
    End Sub

    Public Sub Generate_report(ByVal dt As DataTable, ByVal Nm As String)

        Dim f As New ReportViewer
        Dim rpt As New ReportDocument
        Try
            rpt.Load(Nm)
            rpt.SetDataSource(dt)
            f.CrystalReportViewer1.ReportSource = rpt
            f.Show()
        Catch ex As Exception
            MessageBox.Show("Error: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try


    End Sub

    Private Sub cbTimeSearch_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbTimeSearch.CheckedChanged
        If cbTimeSearch.Checked = True Then
            tpFrom.Enabled = True
            tpTo.Enabled = True
        Else
            tpFrom.Enabled = False
            tpTo.Enabled = False
        End If
    End Sub
End Class